{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check if Matrix Is X-Matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: checkXMatrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #判断矩阵是否是一个 X 矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>如果一个正方形矩阵满足下述 <strong>全部</strong> 条件，则称之为一个 <strong>X 矩阵</strong> ：</p>\n",
    "\n",
    "<ol>\n",
    "\t<li>矩阵对角线上的所有元素都 <strong>不是 0</strong></li>\n",
    "\t<li>矩阵中所有其他元素都是 <strong>0</strong></li>\n",
    "</ol>\n",
    "\n",
    "<p>给你一个大小为 <code>n x n</code> 的二维整数数组 <code>grid</code> ，表示一个正方形矩阵。如果<em> </em><code>grid</code><em> </em>是一个 <strong>X 矩阵 </strong>，返回 <code>true</code> ；否则，返回 <code>false</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/05/03/ex1.jpg\" style=\"width: 311px; height: 320px;\">\n",
    "<pre><strong>输入：</strong>grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>矩阵如上图所示。\n",
    "X 矩阵应该满足：绿色元素（对角线上）都不是 0 ，红色元素都是 0 。\n",
    "因此，grid 是一个 X 矩阵。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/05/03/ex2.jpg\" style=\"width: 238px; height: 246px;\">\n",
    "<pre><strong>输入：</strong>grid = [[5,7,0],[0,3,1],[0,5,0]]\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>矩阵如上图所示。\n",
    "X 矩阵应该满足：绿色元素（对角线上）都不是 0 ，红色元素都是 0 。\n",
    "因此，grid 不是一个 X 矩阵。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == grid.length == grid[i].length</code></li>\n",
    "\t<li><code>3 &lt;= n &lt;= 100</code></li>\n",
    "\t<li><code>0 &lt;= grid[i][j] &lt;= 10<sup>5</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-if-matrix-is-x-matrix](https://leetcode.cn/problems/check-if-matrix-is-x-matrix/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-if-matrix-is-x-matrix](https://leetcode.cn/problems/check-if-matrix-is-x-matrix/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]', '[[5,7,0],[0,3,1],[0,5,0]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            if grid[i][i] == 0 or grid[n - i - 1][i] == 0:\n",
    "                return False\n",
    "            grid[i][i] = 0\n",
    "            grid[n - i - 1][i] = 0\n",
    "        if max(max(grid)) != 0:\n",
    "            return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if (i == j) or (i + j == n-1):\n",
    "                    if grid[i][j] == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j] != 0:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        for r_i, row in enumerate(grid):\n",
    "            for c_i, element in enumerate(row):\n",
    "                if r_i==c_i or r_i+c_i==len(row)-1:  # X线\n",
    "                    if element == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if element != 0:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n=len(grid)\n",
    "        for i,row in enumerate(grid):\n",
    "            for j,x in enumerate(row):\n",
    "                if i == j or (i+j) == (n-1):\n",
    "                    if x == 0:\n",
    "                        return False\n",
    "                elif x :\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid)):\n",
    "                if j==i or j==len(grid)-i-1:\n",
    "                    if grid[i][j]==0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j]!=0:\n",
    "                        return False\n",
    "        return True\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                # 对角线\n",
    "                if i == j or (i + j) == n - 1:\n",
    "                    if grid[i][j] == 0:\n",
    "                        return False\n",
    "                # 其余\n",
    "                else:\n",
    "                    if grid[i][j]:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[i])):\n",
    "                if i==j or i+j==len(grid)-1:\n",
    "                    if grid[i][j]==0:\n",
    "                         return False\n",
    "                else:\n",
    "                    if grid[i][j]!=0:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n=len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if ((i == j or i+j+1==n) and grid[i][j]==0) or  ((i != j and i+j+1!=n) and grid[i][j]!=0):\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if (j == i or j == n - 1 - i):\n",
    "                    if grid[i][j] == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j] != 0:\n",
    "                        return False\n",
    "        return True\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid)):\n",
    "                if i == j or i + j == len(grid)-1:\n",
    "                    if grid[i][j] == 0 or grid[i][j] == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j] != 0:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        l = len(grid)\n",
    "\n",
    "        for i in range(l):\n",
    "            for j in range(l):\n",
    "                if i==j or i==l-j-1:\n",
    "                    if grid[i][j]==0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j]!=0:\n",
    "                        return False\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i, row in enumerate(grid):\n",
    "            for j, x in enumerate(row):\n",
    "                if i == j or (i + j) == (n - 1):\n",
    "                    if x == 0:\n",
    "                        return False\n",
    "                elif x:\n",
    "                    return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            if grid[i][i] == 0 or grid[i][n-1-i] == 0:\n",
    "                return False\n",
    "            grid[i][i] = 0\n",
    "            grid[i][n-1-i] = 0\n",
    "        if max(max(grid)) != 0:\n",
    "            return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i == j or i+j == n-1:\n",
    "                    if grid[i][j] == 0:\n",
    "                        return False\n",
    "                elif grid[i][j] != 0:\n",
    "                    return False \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        #是一个简单的模拟题\n",
    "        n = len(grid)\n",
    "        for hang in range(n):\n",
    "            for lie in range(n):\n",
    "                if hang == lie or hang + lie == n - 1:\n",
    "                    if grid[hang][lie] == 0:\n",
    "                        return False\n",
    "                elif grid[hang][lie] != 0:\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        #res=[[None for _ in range(len(grid[0]))] for _ in range(len(grid))]\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                if i==j or i==len(grid[i])-j-1:\n",
    "                    if grid[i][j]==0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j]!=0:                            \n",
    "                        return False \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        for i, row in enumerate(grid):\n",
    "            for j, v in enumerate(row):\n",
    "                if i == j or i + j == len(grid) - 1:\n",
    "                    if v == 0:\n",
    "                        return False\n",
    "                elif v:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "\n",
    "        m = len(grid) \n",
    "        n = m - 1\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(m):\n",
    "                if i == j and grid[i][j] == 0:\n",
    "                    return False\n",
    "\n",
    "                if grid[i][n] == 0:\n",
    "                    return False\n",
    "\n",
    "                if i != j and n != j and grid[i][j] != 0:\n",
    "                    return False\n",
    "\n",
    "            n -= 1\n",
    "\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                # 是否在对角线上\n",
    "                b1 = (i == j) or (i == n - 1 - j)\n",
    "                # 是否不为0\n",
    "                b2 = bool(grid[i][j])\n",
    "                # 异或\n",
    "                if b1 ^ b2:\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid)):\n",
    "                if i == j or (i + j) == (len(grid) - 1):\n",
    "                    if grid[i][j] == 0:\n",
    "                        return False\n",
    "                elif grid[i][j]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid)):\n",
    "                if ((j==i or i+j==len(grid)-1) and grid[i][j]==0):\n",
    "                    return False\n",
    "                if (j!=i and i+j!=len(grid)-1) and grid[i][j]!=0:\n",
    "                    return False\n",
    "        return True\n",
    "\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if (i+j==n-1 or i==j) and grid[i][j]==0:return False\n",
    "                elif i+j!=n-1 and i!=j and grid[i][j]!=0:return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        for i, row in enumerate(grid):\n",
    "            for j, x in enumerate(row):\n",
    "                if i == j or (i + j) == (len(grid) - 1):\n",
    "                    if x == 0:\n",
    "                        return False\n",
    "                elif x:\n",
    "                    return False\n",
    "        \n",
    "        return True\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if (i == j) or (i + j == n-1):\n",
    "                    if grid[i][j] == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j] != 0:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid)):\n",
    "                if i==j or i+j==len(grid)-1:\n",
    "                    if grid[i][j]==0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j]!=0:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if (i == j) or (i + j + 1 == n):\n",
    "                    if grid[i][j] == 0: return False\n",
    "                else:\n",
    "                    if grid[i][j] != 0: return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i == j or i+j == n-1:\n",
    "                    if grid[i][j] == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j] != 0:\n",
    "                        return False\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        res = True\n",
    "        n = len(grid)\n",
    "        count = 0\n",
    "        total = 0\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i == j or i + j == n - 1:\n",
    "                    if grid[i][j] == 0:\n",
    "                         return False \n",
    "                    count += grid[i][j]\n",
    "                total += grid[i][j]\n",
    "        return total == count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        l = len(grid)\n",
    "        for i in range(l):\n",
    "            if grid[i][i] == 0 or grid[i][l-1-i] == 0:\n",
    "                return False \n",
    "            if any(grid[i][idx] for idx in range(l) if idx != i and idx != l-1-i):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if j == i or j == n - 1 - i:\n",
    "                    if grid[i][j] == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j] != 0:\n",
    "                        return False\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i, row in enumerate(grid):\n",
    "            for j, x in enumerate(row):\n",
    "                if i==j or ( i+j) == ( n - 1):\n",
    "                    if x == 0:\n",
    "                        return False\n",
    "                elif x:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i == j or i + j == n - 1:\n",
    "                    if grid[i][j] == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j] != 0:\n",
    "                        return False\n",
    "        \n",
    "        return True\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[i])):\n",
    "                \n",
    "                if i==j and grid[i][j]==0:\n",
    "                    \n",
    "                    return False\n",
    "                if i+j==len(grid[i])-1 and grid[i][j]==0:\n",
    "                    \n",
    "                    return False\n",
    "                if i!=j and i+j!=len(grid[i])-1 and grid[i][j]!=0:\n",
    "                    \n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        g=[]\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[i])):\n",
    "                if i == j:\n",
    "                    if grid[i][j] == 0:\n",
    "                        g.append(False)\n",
    "                        break\n",
    "                elif j == len(grid[0]) - i - 1:\n",
    "                    if grid[i][j] == 0:\n",
    "                        g.append(False)\n",
    "                        break\n",
    "\n",
    "                else:\n",
    "                    if grid[i][j] != 0:\n",
    "                        g.append(False)\n",
    "                        break\n",
    "        return len(g)==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        return all(all([(((i == j or i + j == len(grid) - 1) and grid[i][j]) or (i != j and i + j != len(grid) - 1 and not grid[i][j])) for j in range(len(grid))]) for i in range(len(grid)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i == j or i + j == n - 1:\n",
    "                    if not grid[i][j]:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j]:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid) -> bool:\n",
    "        \n",
    "        mcnt = 0\n",
    "        cnt = 0\n",
    "        pos = 0\n",
    "\n",
    "        for i in range(len(grid)):\n",
    "            mcnt += sum(grid[i])\n",
    "            if grid[i][pos] != 0 and grid[i][len(grid) - pos - 1] != 0:\n",
    "                cnt += grid[i][pos] + grid[i][len(grid) - pos -1]\n",
    "                pos += 1\n",
    "            else:\n",
    "                return False\n",
    "        if len(grid) % 2 != 0:\n",
    "            cnt -= grid[len(grid) // 2][len(grid) // 2]\n",
    "        if mcnt == cnt:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if (i == j or i + j == n - 1):\n",
    "                    if grid[i][j] == 0: return False\n",
    "                elif grid[i][j] != 0: return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        N = len(grid)\n",
    "        def diag(i,j):\n",
    "            return i == j or i == N - 1 - j\n",
    "        \n",
    "        ans = True\n",
    "        for i in range(N):\n",
    "            for j in range(N):\n",
    "                if diag(i,j):\n",
    "                    ans = ans and grid[i][j] != 0\n",
    "                else:\n",
    "                    ans = ans and grid[i][j] == 0\n",
    "                if not ans:\n",
    "                    return ans\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        for i in range(0,len(grid)):\n",
    "            for j in range(0,len(grid[0])):\n",
    "                if i==j or i+j==len(grid)-1:\n",
    "                    if grid[i][j]==0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j]!=0:\n",
    "                        return False\n",
    "        return True\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = grid.__len__()\n",
    "        for i in range(n):\n",
    "            if grid[i][i] == 0:\n",
    "                return False\n",
    "            if grid[i][n-1-i] == 0:\n",
    "                return False\n",
    "            for j in range(0, n):\n",
    "                if j == i or j == n-1-i:\n",
    "                    continue\n",
    "                if grid[i][j] != 0:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        flag = 1\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i == j or i + j + 1 == n:\n",
    "                    if grid[i][j] == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j] != 0:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for r in range(n):\n",
    "            for c in range(n):\n",
    "                if r == c or r + c == n - 1:\n",
    "                    if grid[r][c] == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[r][c] != 0:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i, row in enumerate(grid):\n",
    "            for j, x in enumerate(row):\n",
    "                if i == j or (i + j) == (n - 1):\n",
    "                    if x == 0:\n",
    "                        return False\n",
    "                elif x:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for r in range(n):\n",
    "            for c in range(n):\n",
    "                if r == c or r + c == n - 1:\n",
    "                    if grid[r][c] == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[r][c] != 0:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            if grid[i][i] == 0 or grid[-i-1][i] == 0:\n",
    "                return False\n",
    "            grid[i][i] = 0\n",
    "            grid[-i-1][i] = 0\n",
    "        if max(max(grid)) != 0:\n",
    "            return False\n",
    "        else:\n",
    "            return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        #res=[[None for _ in range(len(grid[0]))] for _ in range(len(grid))]\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                if i==j or i==len(grid[i])-j-1:\n",
    "                    if grid[i][j]==0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if i!=j  or i!=len(grid[i])-j-1:\n",
    "                        if grid[i][j]!=0:                            \n",
    "                            return False \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "\n",
    "        m = len(grid) \n",
    "        n = m - 1\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(m):\n",
    "                if i == j and grid[i][j] == 0:\n",
    "                    return False\n",
    "\n",
    "                if grid[i][n] == 0:\n",
    "                    return False\n",
    "\n",
    "                if grid[i][j] != 0 and i != j and n != j :\n",
    "                    return False\n",
    "\n",
    "            n -= 1\n",
    "\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i, row in enumerate(grid):\n",
    "            for j, x in enumerate(row):\n",
    "                if i == j or (i + j) == (n - 1):\n",
    "                    if x == 0:\n",
    "                        return False\n",
    "                elif x:\n",
    "                    return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        m,n = len(grid),len(grid[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if (i==j or i==n-j-1) and grid[i][j]==0:return False\n",
    "                if i!=j and i!=n-j-1 and grid[i][j]!=0:return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n=len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                # 对角线上的元素，要么 i=j，要么 i+j=n-1，如果值是 0 直接 false\n",
    "                if i+j==n-1 or i==j:\n",
    "                    if grid[i][j]==0:\n",
    "                        return False\n",
    "                # 非对角线上的元素需要等于 0，如果不为 0 页直接返回 false        \n",
    "                elif grid[i][j]!=0:\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "\n",
    "        for i in range(n):\n",
    "            if  grid[i][i]==0:\n",
    "                return False\n",
    "            if  grid[i][n-i-1] == 0:\n",
    "                return False\n",
    "            grid[i][i],grid[i][n-i-1] = -1,-1\n",
    "        \n",
    "        if n%2:\n",
    "            res = sum([sum(m) for m in grid])\n",
    "            if res != - (2*n-1):\n",
    "                return False\n",
    "            return True\n",
    "        else:\n",
    "            res = sum([sum(m) for m in grid])\n",
    "            if res != - (2*n):\n",
    "                return False\n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "\n",
    "        m = len(grid) \n",
    "        n = m - 1\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(m):\n",
    "                if i == j and grid[i][j] == 0:\n",
    "                    return False\n",
    "\n",
    "                if grid[i][n] == 0:\n",
    "                    return False\n",
    "\n",
    "                if grid[i][j] != 0 and i != j and n != j :\n",
    "                    return False\n",
    "\n",
    "            n -= 1\n",
    "\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i == j or i == (n - j - 1) or j == (n - i - 1):\n",
    "                    if grid[i][j] == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j] != 0:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n=len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i==j or i+j==n-1:\n",
    "                    if grid[i][j]==0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j]!=0:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        up = 0\n",
    "        bottom = n-1\n",
    "        left = 0\n",
    "        right = n-1\n",
    "        sumX = 0\n",
    "        sumRes = 0\n",
    "        for i in range(n):\n",
    "            if grid[up][left] == 0 or grid[up][right] == 0:\n",
    "                return False\n",
    "            sumX = sumX + grid[up][left] + grid[up][right]\n",
    "            # print(grid[left][up],grid[right][up])\n",
    "            if left == right:\n",
    "                sumX -= grid[up][left]\n",
    "            left = left + 1\n",
    "            right = right - 1\n",
    "            up = up + 1\n",
    "            # sumX = sumX + grid[left][up] + grid[right][up]\n",
    "        print(sumX)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                sumRes = sumRes + grid[i][j]\n",
    "        return sumRes == sumX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i == j or i + j == n - 1:\n",
    "                    if grid[i][j] == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j] != 0:\n",
    "                        return False\n",
    "        \n",
    "        return True\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        for i in range(len(grid)):\n",
    "            tmp=grid[i]\n",
    "            for j in range(len(tmp)):\n",
    "                if j==i or j==len(tmp)-i-1:\n",
    "                    if tmp[j]==0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if tmp[j]!=0:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i, row in enumerate(grid):\n",
    "            for j, x in enumerate(row):\n",
    "                if i == j or (i + j) == (n - 1):\n",
    "                    if x == 0:\n",
    "                        return False\n",
    "                elif x:\n",
    "                    return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        l = len(grid)\n",
    "        for i in range(0,l):\n",
    "            if grid[i][i]==0 or grid[i][l-i-1]==0:\n",
    "                return False\n",
    "        for i in range(0,l):\n",
    "            k = min(i, l-i-1)\n",
    "            for j in list(range(0,k))+list(range(k+1, l-k-1))+list(range(l-k,l)):\n",
    "                if grid[i][j]!=0:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        row,col = len(grid),len(grid[0])\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if (i==j or i+j == row-1) and grid[i][j]==0:\n",
    "                    return False\n",
    "                elif not (i==j or i+j == row-1) and not grid[i][j] == 0:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            if grid[i][i] == 0 or grid[i][n-i-1] == 0:\n",
    "                return False\n",
    "            for j in range(n):\n",
    "                if i != j and i + j != n - 1:\n",
    "                    if grid[i][j] != 0:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)-1\n",
    "        for i in range(n+1):\n",
    "            for j in range(n+1):\n",
    "                if grid[i][j]:\n",
    "                    if i != j and i+j != n:\n",
    "                        return False\n",
    "                else:\n",
    "                    if i == j or i+j == n:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i == j or i == n - j - 1:\n",
    "                    if grid[i][j] == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j] != 0:\n",
    "                        return False\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n=len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i==j or i+j==n-1:\n",
    "                    if grid[i][j]==0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j]!=0:\n",
    "                        return False \n",
    "        return True "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i,row in enumerate(grid):\n",
    "            for j,x in enumerate(row):\n",
    "                if i == j or i == n-j-1:\n",
    "                    if x == 0:\n",
    "                        return False\n",
    "                elif x:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        left, right = 0, n - 1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if j == left or j == right:\n",
    "                    if grid[i][j] == 0:\n",
    "                        return False\n",
    "                elif grid[i][j] != 0:\n",
    "                    return False\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n=len(grid)\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i+j==n-1 or i==j:\n",
    "                    if grid[i][j]==0:\n",
    "                        return False\n",
    "                elif grid[i][j]!=0:\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        target = len(grid)-1\n",
    "        for ind_i, num_list in enumerate(grid):\n",
    "            for ind_j, num in enumerate(num_list):\n",
    "                if ind_i == ind_j or (ind_i+ind_j) == target:\n",
    "                    if num == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if num != 0:\n",
    "                        return False\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        #对角线的，i-j=0，i+j=n-1\n",
    "        m = len(grid)\n",
    "        for i in range(m):\n",
    "            for j in range(m):\n",
    "                if (i == j or i+j == m-1 ):\n",
    "                    if  grid[i][j] == 0:\n",
    "                        return False\n",
    "                else:\n",
    "                    if grid[i][j] !=0 :\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        n = len(grid)\n",
    "        for i in range(n):\n",
    "            if grid[i][i]!=0 and grid[n-i-1][i]!=0:\n",
    "                grid[i][i]=0\n",
    "                grid[n-i-1][i]=0\n",
    "            else:\n",
    "                return False\n",
    "        for j in grid:\n",
    "            for k in j:\n",
    "                if k != 0:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkXMatrix(self, grid: List[List[int]]) -> bool:\n",
    "        a = 0\n",
    "        for i in range(len(grid)):\n",
    "            if grid[i][i] != 0 and grid[i][-i-1] !=0:\n",
    "                grid[i][i] = 0\n",
    "                grid[i][-i-1] = 0\n",
    "            else:\n",
    "                return False\n",
    "            a += sum(grid[i])\n",
    "        return a == 0\n",
    "            "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
